<script type="text/javascript"><!--//--><![CDATA[//><!--

    $(document).ready(function() {

        // Setup Form
		$('#form_master').setupForm([
			{ name: 'kode_penjualan', type: 'text', size: 'short', maxlength: 100, noempty:true, readonly:true },
            { name: 'kode_barang', type: 'text', size: 'long', maxlength: 100, noempty:true },
			{ name: 'kd_vendor', type: 'lov', size: 'short',
				onClearButton:function(){
					$(this).setval(''); 
					$('#nama_vendor').setval(''); 
				},
				onActionButton:function(){ 
					LOVVendor({ onSelect:function(row){ 
						$('#kd_vendor').setval(row.kd_vendor); 
						$('#nama_vendor').setval(row.nama_vendor); 
					} }); 
				}
			},
			{ name: 'nama_vendor', type: 'text', size: '24', readonly:true },
			{ name: 'tanggal_penjualan', type: 'date' },
			{ name: 'jenis_bayar', type: 'text', size: 'long', maxlength: 100, noempty:true },
			{ name: 'unit', type: 'text', size: 'long', maxlength: 100, noempty:true },
			{ name: 'discount', type: 'text', value:'0', size: 'short', maxlength: 100, noempty:false },
			{ name: 'total_bayar_view', type: 'numeric', size: '24', readonly:true },
			{ name: 'total_kembalian_view', type: 'numeric', size: '24', readonly:true },
			{ name: 'bayar', type: 'numeric', size: '24' },
			{ name: 'ppn', type: 'text', value:'0', size: 'short', maxlength: 100, noempty:false },
			{ name: 'kode_barang_add', type: 'text', size: '24' },
			{ name: 'nama_barang', type: 'text', size: '24', readonly:true },
			{ name: 'harga_barang', type: 'text', size: '24', readonly:true },
			{ name: 'jumlah_barang', type: 'text', size: '9' },
			{ name: 'discount_barang', type: 'text', size: '9', value:'0' },
			{ name: 'sub_total_barang', type: 'text', size: '24', readonly:true },
			{ name: 'kode_anggota', type: 'text', size: '24' }
			
        ], <?php echo json_encode($data); ?>);
        
		
        <?php if($mode=="edit"){ ?>
        $('#kode_barang').readonly();
        <?php } ?>
		
		// Set Action Button
		var buttons = [{ label: 'Save', icon: 'save', onclick: function() { 
							$('#form_master').submitForm(	'/'+controller+'/ajax_post', 
							{ detail:[	{ table: 'dtl_sample2', field:['kode_penjualan_detail','kd_barang','jumlah','kode_penjualan','sub_total','discount_item']}																	], 
							//onComplete : function(data){ eval('oData = '+data); loadPage('/'+controller+'/edit/?kode_penjualan='+(oData.genid!='' ? oData.genid : $('#kode_penjualan').getval())); }
							onComplete : function(data){ eval('oData = '+data); loadPage('/'+controller+'/index'); }
							}); 
						}},
					   { label: 'Close', icon: 'cancel', onclick: function() { loadPage('/'+controller+'/index'); } }];
		setAction(buttons);
		
		// Create Tab
		$('#tt').tabs({ border:false }); 

	    $("#kode_barang_add").autocomplete({
	      minLength: 0,
	      source: function (request, response) {
	      		var getKB = $('#kode_barang_add').val();
		        $.getJSON(gs_path+'/km_barang/getdata_ac/' + getKB , function (data) {
		            response($.map(data['rows'], function (isi, index) {
		                return {
		                	desc: isi['desc'],
		                    label: isi['label'],
		                    value: isi['value']
		                };
		            }));
		        });
		    },
		  focus: function( event, ui ) {
	        $("#kode_barang_add").val(ui.item.label);
	        return false;
	      },
	      select: function( event, ui ) {
	        $( "#kode_barang_add" ).val( ui.item.label );
	        $( "#nama_barang" ).val( ui.item.desc );
	        $( "#harga_barang" ).val( ui.item.value );
	        $('#jumlah_barang').val('1');
	        $('#sub_total_barang').val(ui.item.value);
	        $('#jumlah_barang').focus();
	 		
	        return false;
	      }
	    })
	    .keypress(function (e,item) {
            if (e.which == 13) {
            	var getKB = $('#kode_barang_add').val();

            	if(getKB == ''){
            		$('#bayar').val('');
            		$('#bayar').focus();
            	}

            	var json = (function () {
				    var json = null;
				    $.ajax({
				        'async': false,
				        'global': false,
				        'url': gs_path+'/km_barang/getdata_ac/' + getKB,
				        'dataType': "json",
				        'success': function (data) {
				            json = data;
				        }
				    });

				    if(json['rows'].length == 1){
				    	$( "#kode_barang_add" ).val(json['rows'][0]['label']);
					    $( "#nama_barang" ).val(json['rows'][0]['desc']);
					    $( "#harga_barang" ).val(json['rows'][0]['value']);
					    $('#jumlah_barang').val('1');
					    $( "#sub_total_barang" ).val(json['rows'][0]['value']);
					    $('#jumlah_barang').focus();
				    }
				    return json;
				})();
            }
        })
	    .data("autocomplete")._renderItem = function(ul,item) {
	      return $("<li>").data('item.autocomplete', item).append("<a>" + item.label + " - " + item.desc + "</a>").appendTo(ul);
	    };

		// Focus at First Object
		$('#form_master').focusFirst();
		$('#kode_penjualan').setval('P<?php echo date("ymdHi"); ?>');
		$('#kode_barang_add').focus();
		$("#tanggal_penjualan").datepicker("setDate", new Date());

		$('#bayar').keyup(function() {
			var total_bayar = $('#total_bayar').val();
			var bayar = $('#bayar').val();
			var total_kembalian = bayar - total_bayar;
  			$('#total_kembalian').setval(total_kembalian);
  			$('#total_kembalian_view').setval(total_kembalian);
		});

    });
//--><!]]></script>
	
<div id="form_master_message" style="display:none;"></div>
<table class="tb_form" style="display:none;" cellpadding="0" cellspacing="0">
<tr>
	<td class="label">UNIT</td>
	<td><select id="unit" name="unit" class="easyui-combobox" style="width:200px;">
	    <option value="KST">KST</option>
	    <option value="RES">RES</option>
	</select>
	</td>
</tr>
<tr>
    <td class="label">No Penjualan</td>
    <td><input id="kode_penjualan" /></td>
</tr>
<tr>
    <td class="label">TOTAL BAYAR</td>
    <td><input id="total_bayar_view" /><input type="hidden" id="total_bayar" name="total_bayar" /></td>
</tr>
<tr>
    <td class="label">BAYAR</td>
    <td><input id="bayar" /></td>
</tr>
<tr>
    <td class="label">TOTAL KEMBALIAN</td>
    <td><input id="total_kembalian_view" /><input type="hidden" id="total_kembalian" name="total_kembalian" /></td>
</tr>
</table>
<div id="tt" class="easyui-tabs" style="min-height:400px;"> 
	<div title="Detail Barang" style="overflow:auto;padding:10px;">  
    	<?php require_once('formdtl_sample2.php'); ?>
    </div> 
    <div title="Detail Transaksi" style="overflow:auto;padding:10px;">
    	<table class="tb_form" style="display:none;" cellpadding="0" cellspacing="0">  
    	<tr>
		    <td class="label">Tanggal Penjualan</td>
		    <td><input id="tanggal_penjualan" /></td>
		</tr>
		    <td class="label">PPN</td>
		    <td><input id="ppn" /></td>
		</tr>
		<tr>
		    <td class="label">Discount</td>
		    <td><input id="discount" /></td>
		</tr>
		<tr>
		    <td class="label">Jenis Pembayaran</td>
		    <td><select id="jenis_bayar" name="jenis_bayar" class="easyui-combobox" style="width:200px;">
				    <option value="Cash">Cash</option>
				    <option value="Credit">Credit</option>
				</select>
		    </td>
		</tr>
		<tr>
		    <td class="label">Kode Anggota</td>
		    <td><input id="kode_anggota" /></td>
		</tr>
		</table>
    </div>
    
</div>